package framework.model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import framework.view.*;

/**
 * All the models have to extend this class, it contains functionality to add and remove listeners as well as 
 * firing of events.
 * @author Stephan Veenstra
 * @version 0.1
 */
abstract public class Model {
	
	private ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();
	
	public Model() {
	}
	
	public void addListener(ActionListener listener){
		this.listeners.add(listener);
	}
	
	public void removeListener(ActionListener listener) {
		if(this.listeners.contains(listener)) {
			this.listeners.remove(listener);
		}
	}
	
	public void dispatchEvent(ActionEvent event){
		try {
			for(ActionListener a : this.listeners){
				a.actionPerformed(event);
			}		
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
}